//
//  FCAlertAction.m
//  FCCommonUse_Example
//
//  Created by WJC on 2018/12/18.
//  Copyright © 2018 64912907. All rights reserved.
//

#import "FCAlertAction.h"
#import "WJCDefine.h"
@interface FCAlertAction()

@end
@implementation FCAlertAction
+(instancetype)actionWithTitle:(NSString *)title style:(FCAlertActionType)style handler:(void (^)(FCAlertAction *))handler{
    FCAlertAction *alert=[[FCAlertAction alloc] initWithStyle:style];
//    [alert setTitle:title];
    [alert setTitle:title forState:UIControlStateNormal];
//    WJCSETLRViewBorderRadius(alert, 20);
    if (handler) {
        alert.alertBlock = ^(FCAlertAction *action) {
            handler(action);
        };
    }
    return alert;
}
-(instancetype)initWithStyle:(FCAlertActionType)style{
    if (self=[super init]) {
        self=[FCAlertAction buttonWithType:UIButtonTypeCustom];
        self.titleLabel.font=font15;
        self.style=style;
        switch (style) {
            case FCAlertActionDefault:
                [self setTitleColor:HEXC(0x238EFA) forState:UIControlStateNormal];
                [self setBackgroundColor:[UIColor whiteColor]];
                
                break;
            case FCAlertActionDelete:
//                [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                [self setTitleColor:HEXC(0xE67373) forState:UIControlStateNormal];
                [self setBackgroundColor:[UIColor whiteColor]];
                self.titleLabel.font=Bfont15;
                break;
            case HXAlertActionDefault:
//                [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [self setBackgroundColor:HEXC(0x3675F9)];
                self.titleLabel.font=font15;
                break;
            case HXAlertActionEdit:
                [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [self setBackgroundColor:HEXC(0xFB9D3A)];
                self.titleLabel.font=Bfont15;
                break;
            case HXAlertActionCancel:
                [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [self setBackgroundColor:HEXC(0xCCCCCC)];
                break;
            case HXAlertActionWhite:
                [self setTitleColor:HEXC(0x333333) forState:UIControlStateNormal];
                [self setBackgroundColor:[UIColor whiteColor]];
                break;
            case HTHAlertActionDefault:
                [self setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
                [self setBackgroundColor:HEXC(0xFEE739)];
                break;
            default:
                [self setTitleColor:HEXC(0xCCCCCC) forState:UIControlStateNormal];
                [self setBackgroundColor: [UIColor whiteColor]];
                break;
        }
//        [self setTitle:title];
        [self addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(void)btnClick:(FCAlertAction*)sender{
    if(self.actionEventAfterDismiss){
        if (self.alertBlock) {
            self.alertBlock(sender);
            
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             [sender.superview.viewController dismissViewControllerAnimated:YES completion:^{
                   }];
        });
       
    }
   if (!self.isNoDismiss) {
        [sender.superview.viewController dismissViewControllerAnimated:YES completion:^{
            if (self.alertBlock) {
                self.alertBlock(sender);
                
            }
        }];
        return;
    }
    
    if (self.alertBlock) {
        
        self.alertBlock(sender);
        
    }
//    else{
//        [sender.superview.viewController dismissViewControllerAnimated:YES completion:nil];
//    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
